package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.entity.BaseResponse;
import com.six666.medicaldevicetestingsystem.entity.SampleList;
import com.six666.medicaldevicetestingsystem.service.SampleList_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.six666.medicaldevicetestingsystem.dto.Sample_Order_DTO;
import com.six666.medicaldevicetestingsystem.service.AccountOrder_Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@CrossOrigin
@RestController
public class Sample_Controller {
    @Autowired
    SampleList_Service sample_list_service;
    @Autowired
    AccountOrder_Service account_order_service;
    @GetMapping("/get_all_sample_order")
    public BaseResponse<List<Sample_Order_DTO>>show_all_sample_order() {
        return new BaseResponse<>(account_order_service.get_all_sample_order());
    }
    @PostMapping("/add_sample")
    public BaseResponse<Boolean> add_sample(@RequestParam Map<String, String> params) {
        int project_id;
        try {
            project_id = Integer.parseInt(params.get("project_id"));
        } catch (Exception e) {
            project_id=4000001;
        }
        String number,name ,type_name ,model ,source;
        number = params.get("number");
        name = params.get("name");
        type_name = params.get("type_name");
        model = params.get("model");
        source = params.get("source");
        return new BaseResponse<>(sample_list_service.add_sample(number,name,type_name,model,source,project_id));
    }
    @PostMapping("/del_sample")
    public BaseResponse<Boolean> del_sample(@RequestParam Map<String, String> params) {
        int sample_id;
        try {
            sample_id = Integer.parseInt(params.get("sample_id"));
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(101,null,"参数错误","sample_id为空或非整形");
        }
        return new BaseResponse<>(sample_list_service.delete_sample(sample_id));
    }

    @PostMapping("/get_sample_by_id")
    public BaseResponse<List<SampleList>> getSampleByNumber(@RequestParam Map<String, String> params) {
        int project_id=Integer.parseInt(params.get("project_id"));
        try {
            List<SampleList> data=sample_list_service.get_sample_by_project_id(project_id);
            return new BaseResponse<>(data);
        }
        catch (Exception e) {
            return new BaseResponse<>(101,null,"参数错误","标准号不存在");
        }
    }
    @PostMapping("/add_sample_order")
    public BaseResponse<Boolean> add_equip_order(@RequestParam Map<String,String> params) {
        int account_id,goods_id,goods_amount;
        try {
            account_id = Integer.parseInt(params.get("account_id"));
            goods_id = Integer.parseInt(params.get("goods_id"));
            goods_amount = Integer.parseInt(params.get("goods_amount"));
        }
        catch (Exception e) {
            e.printStackTrace();
            return new BaseResponse<>(102, null, "参数错误", "account_id、goods_id、goods_amount为空或非整形");
        }
        boolean res=account_order_service.add_order(account_id,"Sample",goods_id,goods_amount);
        return new BaseResponse<>(res);
    }

    @PostMapping("/show_my_order")
    public BaseResponse<List<Sample_Order_DTO>>show_my_order(@RequestParam Map<String,String> params) {
        int account_id=Integer.parseInt(params.get("account_id"));
        String type=params.get("type");
        if(type==null|| type.isEmpty()){
            type="OngoingAndFinish";
        }
        else if(!(type.equals("OngoingAndFinish") || type.equals("Finish") || type.equals("Ongoing") || type.equals("Preformed"))){
            return new BaseResponse<>(101, null, "参数错误", "参数为空或非整形");

        }

        return new BaseResponse<>(account_order_service.get_my_order(account_id,type));
    }

}
